package com.zegome.game.gomestory.main.scene.play.board;


public class CheckEat {
	
	private Board board;	
	private DataMap dataMap;
	
	public CheckEat(Board board)
	{
		this.board = board;
		this.dataMap = board.dataMap;
	}
	
	public boolean checkEat(int r1,int c1,int r2,int c2, boolean saveDirect){	
		
		float wiPika = board.dataMap.wi1Pikachu;
		float hiPika = board.dataMap.hi1Pikachu;
		//float wiLine = board.animationEat.lineConnect.wiLine;
		float xMin, xMax, yMin,yMax;
		xMin = board.dataMap.xBegin + 0.75f*wiPika;
		xMax = board.dataMap.xBegin + (board.dataMap.mapCol+1+0.25f)*wiPika;
		yMin = board.dataMap.yBegin + 0.75f*hiPika;
		yMax = board.dataMap.yBegin + (board.dataMap.mapRow+1+0.25f)*hiPika;
		//if(saveDirect)
			//board.animationEat.lineConnect.resetLine();
		int r,c;
		int rmin,rmax,cmin,cmax;
		int rminLine,rmaxLine;
		int cminLine,cmaxLine;
		int rTarget;
		int cTarget;
		rmin = r1>=r2?r2:r1;
		rmax = r1>=r2?r1:r2;
		cmin = c1>=c2?c2:c1;
		cmax = c1>=c2?c1:c2;		
		if(r1==r2){
			if(checkHor(r1, c1, c2)){
				if(saveDirect)
					//board.animationEat.lineConnect.add(0, dataMap.map[r1][cmin].getX()+wiPika/2, dataMap.map[r1][c1].getY()+hiPika/2, dataMap.map[r1][cmax].getX()+wiPika/2, dataMap.map[r1][c1].getY()+hiPika/2);
				return true;
			}
		}
		if(c1==c2){
			if(checkVer(c1, r1, r2)){
				if(saveDirect)
					//board.animationEat.lineConnect.add(1, dataMap.map[r1][c1].getX()+wiPika/2, dataMap.map[rmax][c1].getY()+hiPika/2, dataMap.map[r1][c1].getX()+wiPika/2, dataMap.map[rmin][c1].getY()+hiPika/2);
				return true;
			}
		}
		// Check L
		if(c1>c2){
			if(checkHor(r1, c1, c2-1)&&checkVer(c2, r2, r1)){
				if(saveDirect){
					//board.animationEat.lineConnect.add(0, dataMap.map[r1][c2].getX()+wiPika/2-wiLine/2, dataMap.map[r1][c1].getY()+hiPika/2, dataMap.map[r1][c1].getX()+wiPika/2, dataMap.map[r1][c1].getY()+hiPika/2);
					//board.animationEat.lineConnect.add(1, dataMap.map[rmin][c2].getX()+wiPika/2, dataMap.map[rmax][c2].getY()+hiPika/2, dataMap.map[rmin][c2].getX()+wiPika/2, dataMap.map[rmin][c2].getY()+hiPika/2);
				}
				return true;
			}
		}
		else{			
			if(checkHor(r1, c1, c2+1)&&checkVer(c2, r2, r1)){
				if(saveDirect){
					//board.animationEat.lineConnect.add(0, dataMap.map[r1][c1].getX()+wiPika/2, dataMap.map[r1][c1].getY()+hiPika/2, dataMap.map[r1][c2].getX()+wiPika/2+wiLine/2, dataMap.map[r1][c1].getY()+hiPika/2);
					//board.animationEat.lineConnect.add(1, dataMap.map[rmin][c2].getX()+wiPika/2, dataMap.map[rmax][c2].getY()+hiPika/2, dataMap.map[rmin][c2].getX()+wiPika/2, dataMap.map[rmin][c2].getY()+hiPika/2);
				}
				return true;
			}
		}
		
		if(c2>c1){
			if(checkHor(r2, c2, c1-1)&&checkVer(c1, r1, r2)){
				if(saveDirect){
					//board.animationEat.lineConnect.add(0, dataMap.map[r2][c1].getX()+wiPika/2-wiLine/2, dataMap.map[r2][c1].getY()+hiPika/2, dataMap.map[r2][c2].getX()+wiPika/2, dataMap.map[r2][c1].getY()+hiPika/2);
					//board.animationEat.lineConnect.add(1, dataMap.map[rmin][c1].getX()+wiPika/2, dataMap.map[rmax][c1].getY()+hiPika/2, dataMap.map[rmin][c1].getX()+wiPika/2, dataMap.map[rmin][c1].getY()+hiPika/2);
				}
				return true;
			}
		}
		else{			
			if(checkHor(r2, c2, c1+1)&&checkVer(c1, r1, r2)){
				if(saveDirect){
					//board.animationEat.lineConnect.add(0, dataMap.map[r2][c2].getX()+wiPika/2, dataMap.map[r2][c1].getY()+hiPika/2, dataMap.map[r2][c1].getX()+wiPika/2+wiLine/2, dataMap.map[r2][c1].getY()+hiPika/2);
					//board.animationEat.lineConnect.add(1, dataMap.map[rmin][c1].getX()+wiPika/2, dataMap.map[rmax][c1].getY()+hiPika/2, dataMap.map[rmin][c1].getX()+wiPika/2, dataMap.map[rmin][c1].getY()+hiPika/2);
				}
				return true;
			}
		}
		rTarget = -1;
		// Check U
		for(r=0;r<=dataMap.mapRow+1;r++){
			if(checkHor(r, cmin-1, cmax+1)){
				if(checkVer(c1, r1, r)&&checkVer(c2, r2, r)){
					if(rTarget==-1){
						rTarget=r;
					}
					else{
						if((Math.abs(rTarget-r1)+Math.abs(rTarget-r2))>(Math.abs(r-r1)+Math.abs(r-r2))){
							rTarget = r;
						}
					}
				}
			}				
		}
		if(rTarget!=-1){
			if(saveDirect){
				if(rTarget==0){
					//board.animationEat.lineConnect.add(0, dataMap.map[r1][cmin].getX()+wiPika/2-wiLine/2, yMin-wiLine/2 , dataMap.map[r2][cmax].getX()+wiPika/2+wiLine/2, yMin-wiLine/2);
					//board.animationEat.lineConnect.add(1, dataMap.map[r1][c1].getX()+wiPika/2, yMin-wiLine/2,dataMap.map[r1][c1].getX()+wiPika/2, dataMap.map[r1][c1].getY()+hiPika/2);
					//board.animationEat.lineConnect.add(1, dataMap.map[r2][c2].getX()+wiPika/2, yMin-wiLine/2,dataMap.map[r2][c2].getX()+wiPika/2, dataMap.map[r2][c2].getY()+hiPika/2);
				}
				else if(rTarget==dataMap.mapRow+1){					
					//board.animationEat.lineConnect.add(0, dataMap.map[r1][cmin].getX()+wiPika/2-wiLine/2, yMax-wiLine/2, dataMap.map[r2][cmax].getX()+wiPika/2+wiLine/2, yMax-wiLine/2);
					//board.animationEat.lineConnect.add(1, dataMap.map[r1][c1].getX()+wiPika/2, dataMap.map[r1][c1].getY()+hiPika/2, dataMap.map[r1][c1].getX()+wiPika/2, yMax-wiLine/2);
					//board.animationEat.lineConnect.add(1, dataMap.map[r2][c2].getX()+wiPika/2, dataMap.map[r2][c2].getY()+hiPika/2, dataMap.map[r2][c2].getX()+wiPika/2, yMax-wiLine/2);
				}
				else{
					//board.animationEat.lineConnect.add(0, dataMap.map[r1][cmin].getX()+wiPika/2-wiLine/2, dataMap.map[rTarget][c1].getY()+hiPika/2, dataMap.map[r2][cmax].getX()+wiPika/2+wiLine/2, dataMap.map[rTarget][c1].getY()+hiPika/2);
					rminLine = r1>rTarget?rTarget:r1;
					rmaxLine = r1>rTarget?r1:rTarget;
					//board.animationEat.lineConnect.add(1, dataMap.map[r1][c1].getX()+wiPika/2, dataMap.map[rmaxLine][c1].getY()+hiPika/2,dataMap.map[r1][c1].getX()+wiPika/2, dataMap.map[rminLine][c1].getY()+hiPika/2);
					rminLine = r2>rTarget?rTarget:r2;
					rmaxLine = r2>rTarget?r2:rTarget;
					//board.animationEat.lineConnect.add(1, dataMap.map[r2][c2].getX()+wiPika/2, dataMap.map[rmaxLine][c2].getY()+hiPika/2,dataMap.map[r2][c2].getX()+wiPika/2, dataMap.map[rminLine][c2].getY()+hiPika/2);
				}
			}
			return true;
		}
		cTarget = -1;
		for(c=0;c<=dataMap.mapCol+1;c++){
			if(checkVer(c, rmin-1, rmax+1)){
				if(checkHor(r1, c1, c)&&checkHor(r2, c2, c)){
					if(cTarget==-1){
						cTarget=c;
					}
					else{
						if((Math.abs(cTarget-c1)+Math.abs(cTarget-c2))>(Math.abs(c-c1)+Math.abs(c-c2))){
							cTarget = c;
						}
					}
				}
			}
		}
		
		
		if(cTarget!=-1){
			if(saveDirect){
				if(cTarget==0){
					//board.animationEat.lineConnect.add(1, xMin+wiLine/2, dataMap.map[rmax][c1].getY()+hiPika/2-wiLine/2, xMin+wiLine/2, dataMap.map[rmin][c1].getY()+hiPika/2+wiLine/2);
					//board.animationEat.lineConnect.add(0, xMin+wiLine/2, dataMap.map[r1][c1].getY()+hiPika/2, dataMap.map[r1][c1].getX()+wiPika/2, dataMap.map[r1][c1].getY()+hiPika/2);
					//board.animationEat.lineConnect.add(0, xMin+wiLine/2, dataMap.map[r2][c2].getY()+hiPika/2, dataMap.map[r2][c2].getX()+wiPika/2, dataMap.map[r2][c2].getY()+hiPika/2);
				}
				else if(cTarget==dataMap.mapCol+1){									
					//board.animationEat.lineConnect.add(1, xMax-wiLine/2-1, dataMap.map[rmax][c1].getY()+hiPika/2-wiLine/2, xMax-wiLine/2-1, dataMap.map[rmin][c1].getY()+hiPika/2+wiLine/2);
					//board.animationEat.lineConnect.add(0, dataMap.map[r1][c1].getX()+wiPika/2, dataMap.map[r1][c1].getY()+hiPika/2, xMax-wiLine/2-1, dataMap.map[r1][c1].getY()+hiPika/2);
					//board.animationEat.lineConnect.add(0, dataMap.map[r2][c2].getX()+wiPika/2, dataMap.map[r2][c2].getY()+hiPika/2, xMax-wiLine/2-1, dataMap.map[r2][c2].getY()+hiPika/2);
				}
				else{
					//board.animationEat.lineConnect.add(1, dataMap.map[rmax][cTarget].getX()+wiPika/2, dataMap.map[rmax][cTarget].getY()+hiPika/2-wiLine/2, dataMap.map[rmax][cTarget].getX()+wiPika/2, dataMap.map[rmin][cTarget].getY()+hiPika/2+wiLine/2);
					cminLine = cTarget>c1?c1:cTarget;
					cmaxLine = cTarget>c1?cTarget:c1;
					//board.animationEat.lineConnect.add(0, dataMap.map[r1][cminLine].getX()+wiPika/2, dataMap.map[r1][c1].getY()+hiPika/2,  dataMap.map[r1][cmaxLine].getX()+wiPika/2, dataMap.map[r1][c1].getY()+hiPika/2);
					cminLine = cTarget>c2?c2:cTarget;
					cmaxLine = cTarget>c2?cTarget:c2;
					//board.animationEat.lineConnect.add(0, dataMap.map[r2][cminLine].getX()+wiPika/2, dataMap.map[r2][c2].getY()+hiPika/2,  dataMap.map[r2][cmaxLine].getX()+wiPika/2, dataMap.map[r2][c2].getY()+hiPika/2);
				}
			}
			return true;
		}
		return false;
	}
	public boolean checkHor(int r,int c1,int c2){
		int cmin,cmax;
		cmin = c1>=c2?c2:c1;
		cmax = c1>=c2?c1:c2;
		if(cmax-cmin==1)
			return true;
		for(int c = cmin+1; c < cmax; c++){
			if(dataMap.map[r][c].type!=-1)
				return false;
		}
		return true;
	}
	public boolean checkVer(int c,int r1,int r2){
		int rmin,rmax;
		rmin = r1>=r2?r2:r1;
		rmax = r1>=r2?r1:r2;
		if(rmax-rmin==1)
			return true;
		for(int r = rmin+1; r < rmax; r++){
			if(dataMap.map[r][c].type!=-1)
				return false;
		}
		return true;
	}
	
}
